package lyl.util;

import java.net.InetAddress;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

import org.safehaus.uuid.UUID;
import org.safehaus.uuid.UUIDGenerator;

import sun.misc.BASE64Encoder;

import com.ucap.dcas.utils.DateTimeUtil;

public class UUID_Util {

	private static UUIDGenerator generator = UUIDGenerator.getInstance();
	private static Random rand = generator.getRandomNumberGenerator();

	public static String getUUID(){
		UUID uuid = generator.generateTimeBasedUUID();	
		return uuid.toString();
	}
	
	public static String getTimeBaseRandom(){
		return DateTimeUtil.getNow() + rand.nextInt(10000);//Math.random();
	}
	
	public static String getBase64ForMD5(String str){
		java.security.MessageDigest md;
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			BASE64Encoder encoder = new BASE64Encoder(); 
			return encoder.encode(md.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) {
		System.out.println("uuid="+getUUID());
//		System.out.println("jobname="+getBase64ForMD5("http://incubator.apache.org/graffito/jcr-mapping/index.html"));
//		System.out.println("jobname="+getBase64ForMD5("http://www.picocontainer.org/introduction.html"));
	}
}
